package test;

import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Locale;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class FontWindow extends JFrame {
	JComboBox comboBoxFontNames;
	JTextField textFieldSize;
	JButton buttonOk,buttonApply;
	ChatterWindows chatterWindows;
	Font fontTemp;
	
	public FontWindow(ChatterWindows chatterWindows) throws HeadlessException {
		super("Font");
		this.chatterWindows = chatterWindows;
		this.getContentPane().setLayout(null);
		
		this.buttonOk = new JButton("Ok");
		this.buttonApply = new JButton("Apply");
		this.textFieldSize = new JTextField();
		String[] fontNames;
		 GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
		    Font[] fonts = e.getAllFonts(); // Get the fonts
		    fontNames = new String[fonts.length];
		    for (int i = 0; i < fontNames.length;i++) {
		      fontNames[i] = fonts[i].getFontName();
		    }
		this.comboBoxFontNames = new JComboBox(fontNames);
		
		
		buttonApply.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				applyAction();
				
			}
		});
		
		buttonOk.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				applyAction();
				FontWindow.this.setVisible(false);
				
			}
		});
		
		
		
		this.comboBoxFontNames.setBounds(5,5,200,25);
		this.textFieldSize.setBounds(220, 5, 50, 23);
		this.buttonOk.setBounds(280, 5, 50, 25);
		this.buttonApply.setBounds(335, 5, 70, 25);
		
		this.textFieldSize.setText("14");
		
		this.getContentPane().add(comboBoxFontNames);
		this.getContentPane().add(textFieldSize);
		this.getContentPane().add(buttonOk);
		this.getContentPane().add(buttonApply);
		
		this.setSize(430,80);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		this.setDefaultCloseOperation(HIDE_ON_CLOSE);
		
		
	}
	
	
	public Font getFontTemp() {
		return fontTemp;
	}


	public void setFontTemp(Font fontTemp) {
		comboBoxFontNames.setSelectedItem(fontTemp.getFontName());
		textFieldSize.setText(fontTemp.getSize()+"");
		this.fontTemp = fontTemp;
	}


	public static void main(String[] args) {
		try {
			Locale.setDefault(new Locale("en"));
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (InstantiationException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (UnsupportedLookAndFeelException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		new FontWindow(null);
	}
	public void applyAction(){
		try {
			int fontSize = Integer.parseInt(textFieldSize.getText());
			Font font = new Font((String) comboBoxFontNames.getSelectedItem(), 0, fontSize);
			chatterWindows.textAreaView.setFont(font);
		} catch (NumberFormatException e) {
			JOptionPane.showMessageDialog(this, "Font size không hợp lệ");
		}
	}
	
}
